package com.hlk.emos.wx.aop;

import com.hlk.emos.wx.config.shiro.ThreadLocalToken;
import com.hlk.emos.wx.utils.Result;
import org.aopalliance.intercept.Joinpoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Objects;

/**
 * @author hlk
 * @version 1.0
 * @date 2021-05-20 9:41
 */
@Aspect
@Component
public class TokenAspect {
    @Autowired
    private ThreadLocalToken threadLocalToken;

    @Pointcut("execution(public * com.hlk.emos.wx.controller.*.*(..))")
    public void aspect() {
    }

    @Around("aspect()")
    public Object arount(ProceedingJoinPoint point) throws Throwable {
        Result result = (Result) point.proceed();
        String token = threadLocalToken.getToken();
        if (Objects.nonNull(token)) {
            result.setToken(token);
        }
        return result;
    }
}
